package com.caigou.modules.data.ui.all

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.data.R
import com.caigou.modules.data.adpter.AllYearItemAdapter
import com.caigou.modules.data.bean.AllYearItemBean
import com.caigou.modules.data.databinding.ActivityAllYearBinding
import com.caigou.modules.data.viewmodel.AllYearViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/29
 */
@Route(path = RouterPath.Data.PAGE_ALL_YEAR)
@AndroidEntryPoint
class AllYearActivity : BaseActivity() {

    private lateinit var binding: ActivityAllYearBinding
    private val allYearViewModel: AllYearViewModel by viewModels()

    private val mAdapter: AllYearItemAdapter by lazy {
        AllYearItemAdapter(mutableListOf())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAllYearBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.allYearLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)

        binding.allYearSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchAllYearList()
            }
        }
        binding.allYearRecyclerView.run {
            layoutManager = LinearLayoutManager(this@AllYearActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@AllYearActivity))
        }

        mAdapter.setOnItemClickListener { _, view, position ->
            val bean = mAdapter.getItem(position)
            ARouter.getInstance().build(RouterPath.Data.PAGE_ALL_YEAR_DETAIL)
                .withInt(AppConstants.DATA_ID, bean.mysteryId)
                .navigation()
        }

        fetchAllYearList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(allYearViewModel.allYearListLiveData, ::handleGetAllYearList)
    }

    private fun handleGetAllYearList(result: Resource<MutableList<AllYearItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.allYearSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.allYearSwipeRefreshLayout.isRefreshing = false
                result.data?.apply {
                    mAdapter.setList(this)
                }
            }
        }
    }

    private fun fetchAllYearList() {
        allYearViewModel.getAllYearList()
    }
}